
package com.seekting.study;

public class MeasureTeset {

    public static void main(String[] args) {

        print(MeasureSpec.MODE_MASK, "MODE_MASK");
        print(~MeasureSpec.MODE_MASK, "~MODE_MASK:");
        print(MeasureSpec.AT_MOST, "AT_MOST:");
        print(MeasureSpec.UNSPECIFIED, "UNSPECIFIED");
        print(MeasureSpec.EXACTLY, "EXACTLY:");

        int size = 1280;
        int mode = MeasureSpec.AT_MOST;
        int value = MeasureSpec.makeMeasureSpec(size, mode);
        print(size, "size");
        print(mode, "mode");
        print(value, "value");

        int getSize = MeasureSpec.getSize(value);
        print(getSize, "getSize");

    }

    private static void print(int value, String tag) {
        System.out.println(toBinaryString(value) + tag);
    }

    private static String toBinaryString(int value) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 31; i >= 0; i--) {
            int shift = 1 << i;
            boolean is = (value & shift) == shift;
            String a = is ? "1" : "0";
            stringBuilder.append(a);
        }

        return stringBuilder.toString();
    }

    public static class MeasureSpec {
        private static final int MODE_SHIFT = 30;
        private static final int MODE_MASK = 0x3 << MODE_SHIFT;

        /**
         * Measure specification mode: The parent has not imposed any constraint
         * on the child. It can be whatever size it wants.
         */
        public static final int UNSPECIFIED = 0 << MODE_SHIFT;

        /**
         * Measure specification mode: The parent has determined an exact size
         * for the child. The child is going to be given those bounds regardless
         * of how big it wants to be.
         */
        public static final int EXACTLY = 1 << MODE_SHIFT;

        /**
         * Measure specification mode: The child can be as large as it wants up
         * to the specified size.
         */
        public static final int AT_MOST = 2 << MODE_SHIFT;

        /**
         * Creates a measure specification based on the supplied size and mode.
         * The mode must always be one of the following:
         * <ul>
         * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
         * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
         * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
         * </ul>
         * 
         * @param size the size of the measure specification
         * @param mode the mode of the measure specification
         * @return the measure specification based on size and mode
         */
        public static int makeMeasureSpec(int size, int mode) {
            return size + mode;
        }

        /**
         * Extracts the mode from the supplied measure specification.
         * 
         * @param measureSpec the measure specification to extract the mode from
         * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
         *         {@link android.view.View.MeasureSpec#AT_MOST} or
         *         {@link android.view.View.MeasureSpec#EXACTLY}
         */
        public static int getMode(int measureSpec) {
            return (measureSpec & MODE_MASK);
        }

        /**
         * Extracts the size from the supplied measure specification.
         * 
         * @param measureSpec the measure specification to extract the size from
         * @return the size in pixels defined in the supplied measure
         *         specification
         */
        public static int getSize(int measureSpec) {
            return (measureSpec & ~MODE_MASK);
        }

        /**
         * Returns a String representation of the specified measure
         * specification.
         * 
         * @param measureSpec the measure specification to convert to a String
         * @return a String with the following format: "MeasureSpec: MODE SIZE"
         */
        public static String toString(int measureSpec) {
            int mode = getMode(measureSpec);
            int size = getSize(measureSpec);

            StringBuilder sb = new StringBuilder("MeasureSpec: ");

            if (mode == UNSPECIFIED)
                sb.append("UNSPECIFIED ");
            else if (mode == EXACTLY)
                sb.append("EXACTLY ");
            else if (mode == AT_MOST)
                sb.append("AT_MOST ");
            else
                sb.append(mode).append(" ");

            sb.append(size);
            return sb.toString();
        }
    }
}
